package com.seari.web.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seari.bean.BaseTaskBean;
import com.seari.main.BaseTaskStarter;
import com.seari.service.BaseTaskService;
import com.seari.utils.CronConverUtil;
import com.seari.web.vo.BaseTaskVO;
import com.seari.web.vo.CronVO;

@Controller
@RequestMapping(value = "/task")
public class BaseTaskController {
	
	@Autowired
	public BaseTaskService baseTaskService;
	
	@Autowired
	public BaseTaskStarter baseTaskStarter;
	
	public static String PATH = "D:/bat/";

	@RequestMapping(value = "/list",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getTaskList(){
		Map<String, Object> result = new HashMap<String, Object>();
		List<BaseTaskBean> baseTask = baseTaskService.getBaseTask();
		result.put("rows", baseTask);
		result.put("total", baseTask.size());
		result.put("code", 200);
		return result;
	}
	
	@RequestMapping(value = "/search",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> searchTaskInfos(@RequestParam(value = "taskName")String taskName){
		Map<String, Object> result = new HashMap<String, Object>();
		List<BaseTaskBean> baseTask = baseTaskService.seachBaseTask(taskName);
		result.put("rows", baseTask);
		result.put("total", baseTask.size());
		result.put("code", 200);
		return result;
	}
	
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addTimTask(BaseTaskVO baseTask){
		Map<String, Object> result = new HashMap<String, Object>();
		BaseTaskBean taskBean = new BaseTaskBean();
		
		CronConverUtil cronUtil = new CronConverUtil();
		CronVO cronVO = cronUtil.CronConversion(baseTask.getInterval(), baseTask.getDate(), baseTask.getDt(),baseTask.getFixedType(),baseTask.getFixedTime());
		
		taskBean.setScriptPath(PATH + baseTask.getScriptPath());
		taskBean.setTaskName(baseTask.getTaskName());
		taskBean.setRestartCycle(cronVO.getCron());
		taskBean.setRestartCycleDesc(cronVO.getCronDesc());
		taskBean.setType(baseTask.getType());
		
		int n = baseTaskService.addTimTask(taskBean);
		if(n > 0) {
			result.put("code", 200);
			baseTaskStarter.restartContainer();
		}else {
			result.put("code", 500);
		}
		return result;
	}
	
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateTimTaskInfo(BaseTaskVO baseTask){
		Map<String, Object> result = new HashMap<String, Object>();
		BaseTaskBean taskBean = new BaseTaskBean();
		
		CronConverUtil cronUtil = new CronConverUtil();
		CronVO cronVO = cronUtil.CronConversion(baseTask.getInterval(), baseTask.getDate(), baseTask.getDt(),baseTask.getFixedType(),baseTask.getFixedTime());
		
		taskBean.setId(baseTask.getId());
		taskBean.setScriptPath(PATH + baseTask.getScriptPath());
		taskBean.setTaskName(baseTask.getTaskName());
		taskBean.setRestartCycle(cronVO.getCron());
		taskBean.setRestartCycleDesc(cronVO.getCronDesc());
		taskBean.setType(baseTask.getType());
		
		int n = baseTaskService.updateTimTask(taskBean);
		if(n > 0) {
			result.put("code", 200);
			baseTaskStarter.restartContainer();
		}else {
			result.put("code", 500);
		}
		return result;
	}
	
	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteTaskInfos(@RequestParam(value = "ids[]")Integer[] ids){
		List<Integer> idsList = Arrays.asList(ids);
		Map<String, Object> result = new HashMap<String, Object>();
		int n = baseTaskService.deleteTimTask(idsList);
		if(n > 0) {
			result.put("code", 200);
			baseTaskStarter.restartContainer();
		}else {
			result.put("code", 500);
		}
		return result;
	}
	
	
	
	@RequestMapping(value = "/baseTask")
	public String TaskList() {
		return "baseTask";
	}
}
